package cn.burnit.view;

import cn.burnit.pojo.Book;
import cn.burnit.service.BookService;
import org.jdesktop.swingx.JXDatePicker;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;

/**
 * 添加图书界面
 */
public class AddBookForm extends JFrame {

    private JLabel bookId,bookName,bookType,translator,publishTime,stock,price,publisher,author;
    private JTextField bookIdField,bookNameField,translatorField,stockField,priceField,publisherField,authorField;
    private JButton btn_Add,btn_Cancel;
    private  JComboBox<String> comboBox;
    final JXDatePicker datepick = new JXDatePicker();
     

    public AddBookForm(){
        Container container = getContentPane();
        container.setLayout(null);

        btn_Add=new JButton("保存");
        btn_Add.setBounds(190,310,80,20);
        btn_Cancel=new JButton("取消");
        btn_Cancel.setBounds(320,310,80,20);
        //取消按钮监听
        btn_Cancel.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });
        //添加按钮监听
        btn_Add.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    addBook(e);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        });

        bookId=new JLabel("图书编号");
        bookId.setFont(new Font("宋体", Font.PLAIN, 16));
        bookId.setBounds(50,50,100,20);
        bookType=new JLabel("图书类型");
        bookType.setFont(new Font("宋体", Font.PLAIN, 16));
        bookType.setBounds(50,90,100,20);
        translator=new JLabel("译者");
        translator.setFont(new Font("宋体", Font.PLAIN, 16));
        translator.setBounds(50,130,100,20);
        publishTime=new JLabel("出版时间");
        publishTime.setFont(new Font("宋体", Font.PLAIN, 16));
        publishTime.setBounds(50,170,100,20);
        stock=new JLabel("库存数量");
        stock.setFont(new Font("宋体", Font.PLAIN, 16));
        stock.setBounds(50,210,100,20);
        bookName=new JLabel("图书名称");
        bookName.setFont(new Font("宋体", Font.PLAIN, 16));
        bookName.setBounds(280,50,100,20);
        author=new JLabel("作者");
        author.setFont(new Font("宋体", Font.PLAIN, 16));
        author.setBounds(280,90,100,20);
        publisher=new JLabel("出版社");
        publisher.setFont(new Font("宋体", Font.PLAIN, 16));
        publisher.setBounds(280,130,100,20);
        price=new JLabel("定价");
        price.setFont(new Font("宋体", Font.PLAIN, 16));
        price.setBounds(280,170,100,20);

        bookIdField=new JTextField();
        bookIdField.setColumns(10);
        bookIdField.setBounds(120,50,130,20);
        String[] ty=new String[]{"文学","理学"};
        comboBox = new JComboBox<>(ty);
        comboBox.setBounds(120,90,130,20);
        translatorField=new JTextField();
        translatorField.setColumns(10);
        translatorField.setBounds(120,130,130,20);
        Date date = new Date();
        // 设置 date日期
        datepick.setDate(date);
        datepick.setBounds(120,170,130,20);

        stockField=new JTextField();
        stockField.setColumns(10);
        stockField.setBounds(120,210,130,20);
        bookNameField=new JTextField();
        bookNameField.setColumns(10);
        bookNameField.setBounds(360,50,130,20);
        authorField=new JTextField();
        authorField.setColumns(10);
        authorField.setBounds(360,90,130,20);
        publisherField=new JTextField();
        publisherField.setColumns(10);
        publisherField.setBounds(360,130,130,20);
        priceField=new JTextField();
        priceField.setColumns(10);
        priceField.setBounds(360,170,130,20);

        container.add(bookId);container.add(bookIdField);
        container.add(bookName);container.add(bookNameField);
        container.add(bookType);container.add(comboBox);
        container.add(author);container.add(authorField);
        container.add(translator);container.add(translatorField);
        container.add(publisher);container.add(publisherField);
        container.add(publishTime);container.add(datepick);
        container.add(price);container.add(priceField);
        container.add(stock);container.add(stockField);
        container.add(btn_Add);container.add(btn_Cancel);


        setTitle("添加图书");
        setSize(600,400);
        setLocationRelativeTo(null);
        setResizable(false);
        setVisible(true);
        //setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }


    private void addBook(ActionEvent e) throws Exception {
        String bookId = this.bookIdField.getText();
        String bookname = this.bookNameField.getText();
        String booktype = (String) this.comboBox.getSelectedItem();
        String author = this.authorField.getText();
        String translator = this.translatorField.getText();
        String publisher = this.publisherField.getText();
        Date date = this.datepick.getDate();
        java.sql.Date publishtime = new java.sql.Date(date.getTime());

        float price = Float.parseFloat(this.priceField.getText());
        int stock = Integer.parseInt(this.stockField.getText());
        Book book = new Book(bookId, bookname, booktype, author, translator, publisher, publishtime, price, stock);
        BookService bookService = new BookService();
        int i=bookService.addBook(book);
        if (i>0){
            JOptionPane.showMessageDialog(null,"添加成功");
            dispose();
        }else {
            JOptionPane.showMessageDialog(null,"添加失败");
        }
    }

   public static void main(String[] args) {
       new AddBookForm();
    }
}
